package com.example.nutrition;

import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class NutritionB extends Activity {

	List<Lebensmittel> list_Lebensmittel = new ArrayList<Lebensmittel>();
	private DataSource datasource;
	public static final String PREFS_NAME = "MyPrefsFile";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        datasource = new DataSource(this);
        
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        boolean firstTime = settings.getBoolean("firstTime", false);
        
        
        if(firstTime){
        	initDBmitLebensmittel(datasource);
        	System.out.println("------------------------hier----------------");
        }
        
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("firstTime", true);
        editor.commit();
        
    }
    
    private void initDBmitLebensmittel(DataSource datasource){
    	try {
    	 	datasource.open();
        	datasource.createLebensmittel("Banane", 95, 36, 9, 0.55, 0.22);
        	datasource.createLebensmittel("Apfel", 52, 6, 7, 0.48, 0.12);
        	//pro 100g
        	datasource.createLebensmittel("Haferflocken", 370, 135, 48, 5.4, 0);
        	datasource.close();
        	
        	Toast.makeText(this, "Daten anlegen", Toast.LENGTH_LONG).show();
		} catch (Exception ex) {
			Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
		}
   
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void eintrag(View view){
    	//Intent intent = new Intent(this, Eintrag.class);
    	
    	list_Lebensmittel.clear();
    	
    
    	try {
			datasource.open();
			list_Lebensmittel = datasource.getAllLebensmittel();
			datasource.close();
			
			for (Lebensmittel x : list_Lebensmittel) {
				System.out.println("Name: "+x.getName());
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
    	
    	setContentView(R.layout.activity_eintrag);
    	ArrayAdapter<Lebensmittel> adapterLebensmittel = new ArrayAdapter<Lebensmittel>(NutritionB.this, android.R.layout.simple_list_item_1,list_Lebensmittel);
    	
    	ListView lvLebensmittel = (ListView) findViewById(R.id.alleLebensmittel);
    	lvLebensmittel.setAdapter(adapterLebensmittel);
    	
    	//startActivity(intent);
    
    }
    
    public void verbrauch(View view){
    	Intent intent = new Intent(this, Verbrauch_Tag.class);
    	//EditText editText = (EditText) findViewById(R.id.edit_message);
    	//String message = editText.getText().toString();
    	//intent.putExtra(EXTRA_MESSAGE, message);
    	startActivity(intent);
    }
    
}
